import Util from "../../utils/util";

const app = getApp()
Component({
  properties: {
    cartDialog: {
      type: Boolean,
      value: false,
    },
    cartList: {
      type: Object,
      value: {},
    },
    bagPrice: {
      type: Number,
      value: 0,
    },
    cartKey: {
      type: String,
      value: '',
    },
    cartBottom: {
      type: Number,
      value: 60,
    }
  },
  data: {},
  attached: function () {
  },
  methods: {

    closeLayer(e) { //关闭
      this.setData({
        cartDialog: false,
        menuDailog: false,
      })
    },

    dialogCartOpt(e) { // 弹窗 购物车 增/减
      const that = this,
        key = e.currentTarget.dataset.key,
        pkey = e.currentTarget.dataset.pkey,
        f = e.currentTarget.dataset.f,
        type = parseInt(e.currentTarget.dataset.type);  // 1 : 增加  0： 减少

      let cartList = Util.getCartList(),
        carts = this.data.cartList

      const cartKey = this.data.cartKey
      if (Util.isEmpty(cartList[cartKey]) || Util.isEmpty(cartList[cartKey][key])) {
        return false;
      }

      if (type === 1) {//新增
        // carts[key].amount += 1;
        if (f == 'sku') {
          carts[key].items[pkey].amount += 1;

          if (cartList[cartKey][key].limit_num > 0 && cartList[cartKey][key].items[pkey].amount > cartList[cartKey][key]) {
            Util.Tips({title: "限购数量" + cartList[cartKey][key].limit_num + '份', hideMask: true})
            return
          }

        } else {
          carts[key].amount += 1;
          if (cartList[cartKey][key].limit_num > 0 && carts[key].amount > cartList[cartKey][key].limit_num) {
            Util.Tips({title: "限购数量" + cartList[cartKey][key].limit_num + '份', hideMask: true})
            return
          }
        }

      } else {
        const min_buy = parseInt(carts[key].min_buy || 1);

        if (f == 'sku') {
          if (carts[key].items[pkey].amount <= min_buy) {//套餐减少
            carts[key].items.splice(pkey, 1);
            if (Util.isEmpty(carts[key].items) || carts[key].items.length == 0) {
              delete carts[key];
            }
          } else {
            carts[key].items[pkey].amount -= min_buy;
          }
        } else {
          if (carts[key].amount <= min_buy) {
            delete carts[key];
            if (Util.isEmpty(carts)) {
              this.setData({
                cartDialog: false
              })
            }
          } else {
            carts[key].amount -= parseInt(min_buy);
          }
        }
        if (Util.isEmpty(carts)) {
          delete cartList[cartKey]
        }
      }

      if (!Util.isEmpty(carts)) {
        cartList[cartKey] = carts
      }


      if (Util.isEmpty(carts)) {
        this.setData({
          cartDialog: false
        })
      }

      this.setData({cartList: carts})
      wx.setStorageSync("carts", JSON.stringify(cartList))
      this.triggerEvent("optCart", carts)
    },

    clearCartDialog(e) { //清空
      this.setData({
        cartList: null,
        cartDialog: false
      })
      wx.removeStorageSync("carts")
      this.triggerEvent("clearCart")
    },

  }
});